﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Microsoft.Telco.Controls
{
    /// <summary>
    /// Control to display the list of received messages
    /// </summary>
    public partial class ReceivedMessagesList : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ReceivedMessagesList"/> class.
        /// </summary>
        public ReceivedMessagesList()
        {
            InitializeComponent();
            Loaded += delegate
            {
                ListReceivedMessages.Height = this.Height - 10;
            };
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Grid messageItem = sender as Grid;
            if (messageItem != null && messageItem.Children.Count > 3)
            {
                Point mousePosition = e.GetPosition(this);
                var messageFromTb = VisualTreeExtensions.GetChild(messageItem, "MessageFromTextBlock") as TextBlock;
                if (messageFromTb != null)
                {
                    MessageSenderNameTextBlock.Text = messageFromTb.Text;
                }

                var fullMessageTb = VisualTreeExtensions.GetChild(messageItem, "FullMessageTextBlock") as TextBlock;
                if (fullMessageTb != null)
                {
                    MessageTextBlock.Text = fullMessageTb.Text;
                }

                var messageDateTimeTb = VisualTreeExtensions.GetChild(messageItem, "MessageDataTimeTextBlock") as TextBlock;
                if (messageDateTimeTb != null)
                {
                    MessageDateTimeTextBlock.Text = messageDateTimeTb.Text;
                }

                MessageDetailsPopup.IsOpen = true;
                MessageDetailsPopup.HorizontalOffset = mousePosition.X;
                MessageDetailsPopup.VerticalOffset = mousePosition.Y;
                e.Handled = true;
            }
        }

        private void Close_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageDetailsPopup.IsOpen = false;
            e.Handled = true;
        }

        #region event handlers
       
        #endregion
    }
}
